﻿namespace TVersityDotNet.MediaLib
{
    using System;
    using System.Globalization;
    using System.Runtime.Serialization;
    using System.Xml.Linq;

    [DataContractAttribute]    
    public class MediaLibVideo : MediaLibMediaItem
    {
        [DataMember]
        public string Genre { get; set; }
        [DataMember]
        public string Album { get; set; }
        [DataMember]
        public string Resolution { get; set; }
        [DataMember]
        public string Publisher { get; set; }
        [DataMember]
        public int NBAudioChannels { get; set; }
        [DataMember]
        public int Bitrate { get; set; }
        [DataMember]
        public int SampleFrequency { get; set; }
        [DataMember]
        public TimeSpan Duration { get; set; }

        public MediaLibVideo(XElement xmlElement)
        {
            this.SearchClass = MediaLibClassType.VideoItem;
            this.RawXml = xmlElement;
            this.ProcessXmlElement();
        }

        public new void ProcessXmlElement()
        {
            base.ProcessXmlElement();
            this.Resolution = this.ResElement.Attribute("resolution").Value;
            this.Publisher = this.RawXml.Element(XName.Get("publisher", MediaLibResponseItem.Namespace_DC)).Value;
            this.Genre = this.RawXml.Element(XName.Get("genre", MediaLibResponseItem.Namespace_Upnp)).Value;            
            this.Album = this.RawXml.Element(XName.Get("album", MediaLibResponseItem.Namespace_Upnp)).Value;
            this.NBAudioChannels = int.Parse(this.ResElement.Attribute("nrAudioChannels").Value, CultureInfo.InvariantCulture);
            this.SampleFrequency = int.Parse(this.ResElement.Attribute("sampleFrequency").Value, CultureInfo.InvariantCulture);
            this.Bitrate = int.Parse(this.ResElement.Attribute("bitrate").Value, CultureInfo.InvariantCulture);
            if (this.ResElement.Attribute("duration").Value != null)
                this.Duration = TimeSpan.Parse(this.ResElement.Attribute("duration").Value, CultureInfo.InvariantCulture);
        }
    }
}
